<?php
/**
 * 工作流程设置
 * Enter description here ...
 * @author cc
 *
 */
namespace Home\Controller;
use Think\Controller;
class WorkFlowSysController extends CommonController{
	public function way(){
		session("action","WorkFlowSys");
		session("menu_id",I('get.menu_id'));
		$this->redirect("Public/index");
	}
	
	public function index(){
		$this->display();
	}
	
	public function nav(){
		
		$this->display();
	}
	
	public function flow_sort(){
	 	$workflow_sort = M("workflow_sort");
	 	$data = $workflow_sort->order('sort_no')->select();
	 	$count = $workflow_sort->count();
	 	$this->assign('data',$data);
	 	$this->assign('count',$count);
	 	$this->display();
	 }
	 
	 public function flow_sort_add(){
	 	$this->display();
	 }
	 
	 public function flow_sort_insert(){
	 	if(!IS_POST){ $this->error("非法表单！");exit; }
	 	$workflow_sort = M("workflow_sort");
	 	$data = array(
	 		'sort_name'=>$_POST['sort_name'],
	 		'sort_no'=>$_POST['sort_no']
	 	);
	 	$success = $workflow_sort->add($data);
	 	if($success){
	 		$this->systemlog('add', '工作流设置', '添加流程分类---'.$_POST['sort_name']);
	 		$this->success('添加流程分类成功','flow_sort');
	 	}else{
	 		$this->error('添加流程分类失败');
	 	}
	 }
	 
	 public function flow_sort_edit(){
	 	$sort_id = I('get.sort_id');
	 	if(!is_id($sort_id)){ $this->error("非法参数！"); }
	 	$workflow_sort = M("workflow_sort");
	 	$data = $workflow_sort->where('sort_id=%d',$sort_id)->find();
	 	$this->assign('data',$data);
	 	$this->display();
	 }
	 
	 public function flow_sort_save(){
	 	if(!IS_POST){ $this->error("非法表单！");exit; }
	 	$workflow_sort = M("workflow_sort");
	 	$data = array(
	 		'sort_name'=>$_POST['sort_name'],
	 		'sort_no'=>$_POST['sort_no']
	 	);
	 	$sort_id = I('post.sort_id');
	 	if(!is_id($sort_id)){ $this->error("非法参数！"); }
	 	$success = $workflow_sort->where('sort_id='.$sort_id)->save($data);
	 	if($success){
	 		$this->systemlog('edit', '工作流设置', '修改流程分类---'.$_POST['sort_name']);
	 		$this->success('修改流程分类成功','flow_sort');
	 	}else{
	 		$this->error('修改流程分类失败');
	 	}
	 }
	 
	 public function flow_sort_delete(){
	 	$workflow_sort = M("workflow_sort");
	 	$workflow_form = M("workflow_form");
	 	$workflow_type = M("workflow_type");
	 	$sort_id = I('get.sort_id');
	 	if(!is_id($sort_id)){ $this->error("非法参数！"); }
	 	
	 	$sort_name = $workflow_sort->where('sort_id=%d',$sort_id)->getField('sort_name');
	 	$success = $workflow_sort->where('sort_id=%d',$sort_id)->delete();
	 	if($success){
	 		$workflow_type->where('sort_id=%d',$sort_id)->setField("sort_id",0);
	 		$workflow_form->where('sort_id=%d',$sort_id)->setField("sort_id",0);
	 		$this->systemlog('delete', '工作流设置', '删除流程分类---'.$sort_name);
	 		$this->success('删除流程分类成功','flow_sort');
	 	}else{
	 		$this->error('删除流程分类失败');
	 	}
	 }
	 /***** 流程分类   end ******/
	 
	 /***** 设计流程   starting ******/
	 
	 public function flow_type(){
	 	$workflow_type = M("workflow_type");
	 	$workflow_sort = M("workflow_sort");
	 	$data = $workflow_sort->order('sort_no')->select();
 		$data[] = array(
 			'sort_id'=>0,
 			'sort_name'=>'未分类流程',
 		);
	 	$this->assign('data',$data);
	 	$this->display();
	 }
	 public function flow_type_list(){
	 	$workflow_type = M("workflow_type");
	 	$workflow_sort = M("workflow_sort");
	 	$data_sort = $workflow_sort->order('sort_no')->select();
	 	$data = array();
	 	$i = 0;
	 	foreach ($data_sort as $key=>$value){
	 		$sort_id = $value['sort_id'];
	 		$data_type = $workflow_type->where('sort_id='.$sort_id)->select();
	 		foreach ($data_type as $key=>$value2){
	 			$value['one'][$key] = $value2;
	 		}
	 		$data[] = $value;
	 		$i++;
	 	}
	 	$data_type0 = $workflow_type->where('sort_id=0')->select();
	 	if(count($data_type0)>0){//当存在未分组的流程
	 		$data[$i] = array(
	 			'sort_id'=>0,
	 			'sort_name'=>'未分类流程',
	 			'one'=>$data_type0
	 		);
	 	}
	 	$this->assign('data',$data);
	 	$this->display();
	 }
	 
	 public function flow_type_add(){
	 	$workflow_sort = M("workflow_sort");
	 	$workflow_form = M("workflow_form");
	 	$data_sort = $workflow_sort->order('sort_no')->select();
	 	$data_form = $workflow_form->select();
	 	$this->assign('data_sort',$data_sort);
	 	$this->assign('data_form',$data_form);
	 	$this->display();
	 }
	 
	 public function flow_type_sort_list(){//流程分类下流程列表
	 	$sort_id = I('get.sort_id') ? I('get.sort_id') : 1;
	 	if(!is_id($sort_id)){ $this->error("非法参数！"); }
	 	
	 	$workflow_type = M("workflow_type");
	 	$workflow_sort = M("workflow_sort");
	 	$data_type = $workflow_type->where('sort_id=%d',$sort_id)->order('type_no')->select();
	 	if($sort_id == 0){
	 		$data_sort = array(
	 			'sort_id'=>0,
	 			'sort_name'=>'未分组流程'
	 		);
	 	}else{
		 	$data_sort = $workflow_sort->where('sort_id=%d',$sort_id)->find();
	 	}
	 	$this->assign('data_type',$data_type);
	 	$this->assign('data_sort',$data_sort);
	 	$this->assign('count',count($data_type));
	 	$this->display();
	 }
	 
	 public function flow_type_insert(){
	 	if(!IS_POST){ $this->error("非法表单！");exit; }
	 	$data = array(
	 		'sort_id'=>$_POST['sort_id'],
	 		'type_no'=>$_POST['type_no'],
	 		'type_name'=>$_POST['type_name'],
	 		'form_id'=>$_POST['form_id'],
	 		'type_explain'=>$_POST['type_explain']
	 	);
	 	$workflow_type = M("workflow_type");
	 	$success = $workflow_type->add($data);
	 	if($success){
	 		$this->systemlog('add', '工作流设置', '添加流程---'.$_POST['type_name']);
	 		$this->success('添加流程成功','flow_type_add');
	 	}else{
	 		$this->error('添加流程失败');
	 	}
	 }

	 public function flow_type_edit(){
	 	$type_id = I('get.type_id');
	 	if(!is_id($type_id)){ $this->error("非法参数！"); }
	 	
	 	$workflow_sort = M("workflow_sort");
	 	$workflow_type = M("workflow_type");
	 	$workflow_form = M("workflow_form");
	 	$data_type = $workflow_type->where('type_id=%d',$type_id)->find();
	 	$data_form = $workflow_form->select();
	 	$data_sort = $workflow_sort->select();
	 	$this->assign('data_type',$data_type);
	 	$this->assign('data_form',$data_form);
	 	$this->assign('data_sort',$data_sort);
	 	$this->display();
	 }
	 
	 public function flow_type_save(){
	 	if(!IS_POST){ $this->error("非法表单！");exit; }
		$data = array(
	 		'sort_id'=>$_POST['sort_id'],
	 		'type_no'=>$_POST['type_no'],
	 		'type_name'=>$_POST['type_name'],
	 		'form_id'=>$_POST['form_id'],
	 		'type_explain'=>$_POST['type_explain']
	 	);
	 	$type_id = I('post.type_id');
	 	if(!is_id($type_id)){ $this->error("非法参数！"); }
	 	$workflow_type = M("workflow_type");
	 	$success = $workflow_type->where('type_id=%d',$type_id)->save($data);
	 	if($success){
	 		$this->systemlog('edit', '工作流设置', '修改流程---'.$_POST['type_name']);
	 		$this->success('修改流程成功','flow_type_edit?type_id='.$type_id);
	 	}else{
	 		$this->error('修改流程失败');
	 	}
	 }
	 
	 public function flow_type_delete(){
	 	$type_id = I('get.type_id');
	 	$sort_id = I('get.sort_id');
	 	if(!is_id($sort_id) || !is_id($type_id)){ $this->error("非法参数！"); }
	 	
	 	$workflow_type = M("workflow_type");
		$type_name = $workflow_type->where('type_id=%d',$type_id)->getField('type_name');
		$success = $workflow_type->where('type_id='.$type_id)->delete();
	 	if($success){
	 		$this->systemlog('delete', '工作流设置', '删除流程---'.$type_name);
	 	}
	 }
	 
	 public function flow_type_disable(){
	 	$type_id = I('get.type_id');
	 	$sort_id = I('get.sort_id');
	 	$status = I('get.status');
	 	
	 	if(!is_id($sort_id) || !is_id($type_id) ){ $this->error("非法参数！"); }
	 	if($status != "0" && $status != "1"){ $this->error("非法参数！"); }
	 	if( $status == "0"){
	 		$action = "禁用";
	 	}else{
	 		$action = "启用";
	 	}
	 	$workflow_type = M("workflow_type");
	 	$type_name = $workflow_type->where('type_id=%d',$type_id)->getField('type_name');
		$success = $workflow_type->where('type_id=%d',$type_id)->setField('status', $status);
	 	if($success){
	 		$this->systemlog('edit', '工作流设置', $action.'流程---'.$type_name);
	 		$this->ajaxReturn($action."成功！");
	 	}else{
	 		$this->ajaxReturn($action."失败！");
	 	}
	 }
	 
	 public function flow_process(){//设计流程步骤
	 	$type_id = I('get.type_id');
	 	if(!is_id($type_id)){ $this->error("非法参数！"); }
	 	
	 	$workflow_type = M("workflow_type");
	 	$workflow_form = M("workflow_form");
	 	$workflow_proc = M("workflow_process");
//	 	$form_id = $workflow_type->where('type_id='.$type_id)->getField('form_id');
	 	$data_proc = $workflow_proc->where('type_id=%d',$type_id)->order('proc_no')->select();
	 	$type_name = $workflow_type->where('type_id=%d',$type_id)->getField('type_name');
	 	$this->assign('data_list',$data_proc);
	 	$this->assign('type_id',$type_id);
	 	$this->assign('type_name',$type_name);
	 	$this->assign("empty", emptyhtml("list"));
	 	$this->display();
	 }
	 
	 public function flow_process_add(){
	 	$type_id = I('get.type_id');
	 	if(!is_id($type_id)){ $this->error("非法参数！"); }
	 	
	 	$workflow_proc = M("workflow_process");
	 	$data_proc_all = $workflow_proc->where('type_id=%d',$type_id)->order('proc_no')->select();
	 	$this->assign('data_proc_all',$data_proc_all);
	 	$this->assign("proc_no", $data_proc_all[count($data_proc_all)-1]['proc_no']+1);
	 	$this->assign('type_id',$type_id);
	 	$this->display();
	 }
	 
	 public function flow_process_insert(){
	 	if(!IS_POST){ $this->error("非法表单！"); }
	 	$type_id = I('post.type_id');
	 	$proc_no = I('post.proc_no');
	 	if(!is_id($type_id) || !is_id($proc_no)){ $this->error("非法参数！"); }
	 	
	 	$workflow_proc = M("workflow_process");
	 	$data_proc = $workflow_proc->where('type_id=%d AND proc_no=%d', array($type_id, $proc_no))->find();
	 	if($data_proc==null){
	 		if(I('post.proc_to') == null){
	 			$data = array(
			 		'proc_no'=>$proc_no,
			 		'proc_name'=>I('post.proc_name'),
			 		'type_id'=>$type_id
		 		);
	 		}else{
			 	$data = array(
			 		'proc_no'=>$proc_no,
			 		'proc_name'=>I('post.proc_name'),
			 		'proc_to'=>I('post.proc_to'),
			 		'type_id'=>$type_id
			 	);
	 		}
		 	$success = $workflow_proc->add($data);
		 	if($success){
		 		$this->systemlog('add', '工作流设置', '添加流程步骤---'.$_POST['proc_name']);
		 		$this->success('添加流程步骤成功','flow_process?type_id='.$type_id);
		 	}else{
		 		$this->error('添加流程步骤失败');
		 	}
	 	}else{
	 		$this->error('步骤排序号重复');
	 	}
	 }
	 
	 public function flow_process_edit(){
	 	$type_id = I('get.type_id');
	 	$proc_id = I('get.proc_id');
	 	if(!is_id($type_id) || !is_id($proc_id)){ $this->error("非法参数！"); }
	 	$workflow_proc = M("workflow_process");
	 	$data_proc_select = array();
	 	$data_proc_all = $workflow_proc->where('type_id=%d',$type_id)->order('proc_no')->select();
	 	$data_proc = $workflow_proc->where('proc_id=%d',$proc_id)->find();
//	 	dump($data_proc);
	 	if($data_proc['proc_to'] != null && strlen($data_proc['proc_to']) >1){
	 		if($data_proc['proc_to'] == 0){
	 			$data_proc_select['proc_no']=0;
	 			$data_proc_select['proc_name']='[结束流程]';
	 		}else{
		 		$data_proc_select =$workflow_proc->where('proc_no='.$data_proc['proc_to'].' AND type_id='.$type_id)->find(); 
//		 		echo $workflow_proc->getLastSql();
	 		}
	 	}else if($data_proc['proc_to'] == null){
	 		$data_proc_select = '';
	 	}else{
	 		$data_proc_select =$workflow_proc->where('proc_no='.$data_proc['proc_to'].' AND type_id='.$type_id)->find();
	 	}
//	 	dump($data_proc_select);exit;
	 	$this->assign('data_proc',$data_proc);
	 	$this->assign('data_proc_select',$data_proc_select);
	 	$this->assign('data_proc_all',$data_proc_all);
	 	$this->assign('type_id',$type_id);
	 	$this->display();
	 }
	 
	 public function flow_process_save(){
	 	if(!IS_POST){ $this->error("非法表单！"); }
	 	$type_id = I('post.type_id');
	 	$proc_no = I('post.proc_no');
	 	$proc_to = I('post.proc_to');
	 	$proc_id = I('post.proc_id');
	 	if(!is_id($type_id) || !is_id($proc_no) || !is_id($proc_id)){ $this->error("非法参数！"); }
	 	
	 	$workflow_proc = M("workflow_process");
	 	
		$data_proc = $workflow_proc->where('type_id=%d AND proc_no=%d AND proc_id != %d', array($type_id,$proc_no,$proc_id))->find();
	 	if($data_proc==null){
		 	if($proc_to == null){
			 	$data = array(
			 		'proc_no'=>$proc_no,
			 		'proc_name'=>I('post.proc_name'),
			 		'proc_to'=>'0',
			 	);
		 		
		 	}else{
		 		$data = array(
			 		'proc_no'=>$proc_no,
			 		'proc_name'=>I('post.proc_name'),
			 		'proc_to'=>$proc_to,
			 	);
		 	}
		 	$success = $workflow_proc->where('proc_id=%d',$proc_id)->save($data);
		 	if($success){
		 		$this->systemlog('edit', '工作流设置', '修改流程步骤---'.I('post.proc_name'));
		 		$this->success('修改流程步骤成功','flow_process?type_id='.$type_id);
		 	}else{
		 		$this->error('修改流程步骤失败');
		 	}
	 	}else{
	 		$this->error('步骤排序号重复');
	 	}
	 }
	 
	 public function flow_process_delete(){
	 	$type_id = I('get.type_id');
	 	$proc_id = I('get.proc_id');
	 	if(!is_id($type_id) || !is_id($proc_id)){ $this->error("非法参数！"); }
	 	
	 	$workflow_proc = M("workflow_process");
	 	$proc_name = $workflow_proc->where('proc_id=%d',$proc_id)->getField('proc_name');
		$success = $workflow_proc->where('proc_id=%d',$proc_id)->delete();
	 	if($success){
	 		$this->systemlog('delete', '工作流设置', '删除流程步骤---'.$proc_name);
	 	}
	 }
	 
	 public function flow_process_item(){//编辑可写字段
	 	$type_id = I('get.type_id');
	 	$proc_id = I('get.proc_id');
	 	if(!is_id($type_id) || !is_id($proc_id)){ $this->error("非法参数！"); }
	 	
	 	$workflow_proc = M("workflow_process");
	 	$workflow_form = M("workflow_form");
	 	$workflow_type = M("workflow_type");
	 	$form_id = $workflow_type->where('type_id=%d',$type_id)->getField('form_id');
	 	$data_form = $workflow_form->where('form_id=%d',$form_id)->find();//查出表单模型
	 	$form_model = html_element($data_form['form_model']);//将表单按照元素转换成数组
//	 	$form_count = count($form_model);
		$bak = array();
	 	foreach ($form_model as $key=>$value){
	 		if(!in_array($value, $bak)){//去掉多余重复的元素
		 		$title = explode(" ",strstr($value, 'title='));//截取title开头之后的值，
		 		$form_item[] = substr($title[0], '6');//获取title的值
	 			$bak[] = $value;
	 		}
	 	}
	 	$data_proc = $workflow_proc->where('proc_id='.$proc_id)->find();
	 	if($data_proc['proc_item'] != null){
	 		$proc_item_select = explode(',', $data_proc['proc_item']);
	 	}
	 	$this->assign('form_item',$form_item);
	 	$this->assign('data_proc',$data_proc);
 		$this->assign('proc_item_select',$proc_item_select);
 		$this->display();
	 }
	 
	 public function flow_process_item_save(){
	 	if(!IS_POST){ $this->error("非法表单！"); }
	 	$type_id = I('post.type_id');
	 	$proc_id = I('post.proc_id');
	 	if(!is_id($type_id) || !is_id($proc_id)){ $this->error("非法参数！"); }
	 	
	 	if($_POST['proc_item']==null){
	 		$data['proc_item'] = '';
	 	}else{
		 	$data['proc_item'] = trim($_POST['proc_item'],',');
	 	}
	 	$workflow_proc = M("workflow_process");
	 	$proc_name = $workflow_proc->where('proc_id=%d',$proc_id)->getField("proc_name");
	 	$success = $workflow_proc->where('proc_id=%d',$proc_id)->save($data);
	 	if($success){
	 		$this->systemlog('edit', '工作流设置', '修改流程步骤---'.$proc_name);
	 		$this->success('修改流程步骤成功','flow_process?type_id='.$type_id);
	 	}else{
	 		$this->error('修改流程步骤失败');
	 	}
	 }
	 
	 public function flow_process_condition(){//设置条件
	 	$type_id = I('get.type_id');
	 	$proc_id = I('get.proc_id');
	 	if(!is_id($type_id) || !is_id($proc_id)){ $this->error("非法参数！"); }
	 	
	 	$workflow_proc = M("workflow_process");
	 	$workflow_form = M("workflow_form");
	 	$workflow_type = M("workflow_type");
	 	$form_id = $workflow_type->where('type_id=%d',$type_id)->getField('form_id');
	 	$data_form = $workflow_form->where('form_id=%d',$form_id)->find();//查出表单模型
	 	$data_proc = $workflow_proc->where('proc_id=%d',$proc_id)->find();
	 	$form_model = html_element($data_form['form_model']);
//	 	$form_count = count($form_model);
		$bak = array();
	 	foreach ($form_model as $key=>$value){
	 		if(!in_array($value, $bak)){//去掉多余重复的元素
		 		$title = explode(" ",strstr($value, 'title='));//截取title开头之后的值，
		 		$form_item[] = substr($title[0], '6');//获取title的值
	 			$bak[] = $value;
	 		}
	 	}
	 	$this->assign('data_proc',$data_proc);
	 	$this->assign('form_item',$form_item);
	 	$this->display();
	 }
	 
	 public function flow_process_condition_save(){
	 	if(!IS_POST){ $this->error("非法表单！"); }
	 	$type_id = I('post.type_id');
	 	$proc_id = I('post.proc_id');
	 	if(!is_id($type_id) || !is_id($proc_id)){ $this->error("非法参数！"); }
	 	
	 	$data = array(
	 		'proc_in'=>$_POST['proc_in'],
	 		'proc_out'=>$_POST['proc_out'],
	 	);
	 	$workflow_proc = M("workflow_process");
	 	$proc_name = $workflow_proc->where('proc_id=%d',$proc_id)->getField("proc_name");
	 	$success = $workflow_proc->where('proc_id='.$proc_id)->save($data);
	 	if($success){
	 		$this->systemlog('edit', '工作流设置', '修改流程设置条件---'.$proc_name);
	 		$this->success('修改设置条件成功','flow_process?type_id='.$type_id);
	 	}else{
	 		$this->error('修改设置条件失败');
	 	}
	 }
	 
	 public function flow_process_user(){//经办人员
	 	$type_id = I('get.type_id');
	 	$proc_id = I('get.proc_id');
	 	if(!is_id($type_id) || !is_id($proc_id)){ $this->error("非法参数！"); }
	 	
	 	$workflow_proc = M("workflow_process");
	 	$dept = M("sys_dept");
	 	$user = M("sys_user");
	 	$data_user = $user->select();
	 	$proc_user = $workflow_proc->where('proc_id=%d',$proc_id)->getField('proc_user');
	 	$i = 0;
	 	if($proc_user != '' || $proc_user != null){
	 		$proc_user = explode(",",$proc_user);
		 	foreach ($data_user as $key1=>$value1){//循环出所有的用户
		 		if(!in_array($value1['user_name'],$proc_user)){
			 		$dept_id = $value1['dept_id'];
			 		$data_user_new[$i] = $value1;
			 		$data_user_new[$i]['dept_name'] = $dept->where('id='.$dept_id)->getField('dept_name');
			 		$i++;
		 		}
		 	}
		 	foreach ($proc_user as $key=>$value){//循环查出经办人员
		 		$data_user1 = $user->where("user_name='$value'")->find();
		 		$dept_id = $data_user1['dept_id'];
		 		$proc[$key]['dept_name'] = $dept->where('id='.$dept_id)->getField('dept_name');
		 		$proc[$key]['name'] = $data_user1['name'];
		 		$proc[$key]['user_name'] = $value;
		 	}
	 	}else{
		 	foreach ($data_user as $key=>$value){
		 		$dept_id = $value['dept_id'];
		 		$data_user_new[$key] = $value;
		 		$data_user_new[$key]['dept_name'] = $dept->where('id='.$dept_id)->getField('dept_name');
		 	}
	 	}
//	 	dump($data_user_new);
	 	$data_proc = $workflow_proc->where('proc_id='.$proc_id)->find();
	 	$this->assign('data_proc',$data_proc);
	 	$this->assign('proc_user',$proc);
	 	$this->assign('data_user',$data_user_new);
	 	$this->display();
	 }
	 
	 public function flow_process_user_save(){
	 	if(!IS_POST){ $this->error("非法表单！"); }
	 	$type_id = I('post.type_id');
	 	$proc_id = I('post.proc_id');
	 	if(!is_id($type_id) || !is_id($proc_id)){ $this->error("非法参数！"); }
	 	
	 	$workflow_proc = M("workflow_process");
	 	if($_POST['proc_user'] == null){
	 		$data['proc_user'] = '';
	 	}else{
		 	$data['proc_user'] = trim($_POST['proc_user'],",");
	 	}
	 	$proc_name = $workflow_proc->where('proc_id=%d',$proc_id)->getField("proc_name");
	 	$success = $workflow_proc->where('proc_id=%d',$proc_id)->save($data);
	 	if($success){
	 		$this->systemlog('edit', '工作流设置', '修改流程经办人员---'.$proc_name);
	 		$this->success('修改流程步骤成功','flow_process?type_id='.$_POST['type_id']);
	 	}else{
	 		$this->error('修改流程步骤失败');
	 	}
	 }
	 /*****经办部门、经办角色 start*****/
	 public function flow_process_dept(){
	 	$type_id = $_GET['_URL_']['3'];
	 	$proc_id = $_GET['_URL_']['5'];
	 	$workflow_proc = M("Oa_workflow_process");
	 	$dept = M("Oa_sys_dept");
 		$data_dept = $dept->select();
	 	$data_proc = $workflow_proc->where('proc_id='.$proc_id)->find();
	 	if($data_proc['proc_dept'] != null){
	 		$dept_id = explode(',', $data_proc['proc_dept']);
	 		foreach ($data_dept as $key1=>$value1){
	 			if(!in_array($value1['id'], $dept_id)){
	 				$data_dept_bak[] = $value1;
	 			}
	 		}
	 		foreach ($dept_id as $key=>$value){//查询经办部门
	 			$proc_dept[] = $dept->where('id='.$value)->find();
	 		}
	 	}else{
	 		$data_dept_bak = $data_dept;
	 	}
	 	$this->assign('data_dept',$data_dept_bak);
	 	$this->assign('proc_dept',$proc_dept);
	 	$this->assign('data_proc',$data_proc);
	 	$this->display();
	 }
	 
	 public function flow_process_dept_save(){
	 	$workflow_proc = M("Oa_workflow_process");
	 	if($_POST['proc_dept'] == null){
	 		$data['proc_dept'] = '';
	 	}else{
		 	$data['proc_dept'] = substr($_POST['proc_dept'],0,-1);
	 	}
	 	$proc_id = $_POST['proc_id'];
	 	$success = $workflow_proc->where('proc_id='.$proc_id)->save($data);
	 	if($success){
	 		$log = new SystemAction();
	 		$log->log(session('name'), '修改', '工作流设置', '修改流程步骤');
	 		$this->success('修改流程步骤成功','__URL__/flow_process/type/'.$_POST['type_id']);
	 	}else{
	 		$this->error('修改流程步骤失败');
	 	}
	 }
	 
	 public function flow_process_priv(){
	 	$type_id = $_GET['_URL_']['3'];
	 	$proc_id = $_GET['_URL_']['5'];
	 	$workflow_proc = M("Oa_workflow_process");
	 	$priv = M("Oa_sys_priv");
 		$data_priv = $priv->select();
	 	$data_proc = $workflow_proc->where('proc_id='.$proc_id)->find();
	 	if($data_proc['proc_priv'] != null){
	 		$priv_id = explode(',', $data_proc['proc_priv']);
	 		foreach ($data_priv as $key1=>$value1){
	 			if(!in_array($value1['id'], $priv_id)){
	 				$data_priv_bak[] = $value1;
	 			}
	 		}
	 		foreach ($priv_id as $key=>$value){//查询经办部门
	 			$proc_priv[] = $priv->where('id='.$value)->find();
	 		}
	 	}else{
	 		$data_priv_bak = $data_priv;
	 	}
	 	$this->assign('data_priv',$data_priv_bak);
	 	$this->assign('proc_priv',$proc_priv);
	 	$this->assign('data_proc',$data_proc);
	 	$this->display();
	 }
	 
	 public function flow_process_priv_save(){
	 	$workflow_proc = M("Oa_workflow_process");
//	 	echo $_POST['proc_priv'];
	 	if($_POST['proc_priv'] == null){
	 		$data['proc_priv'] = '';
	 	}else{
		 	$data['proc_priv'] = substr($_POST['proc_priv'],0,-1);
	 	}
	 	$proc_id = $_POST['proc_id'];
	 	$success = $workflow_proc->where('proc_id='.$proc_id)->save($data);
	 	if($success){
	 		$log = new SystemAction();
	 		$log->log(session('name'), '修改', '工作流设置', '修改流程步骤');
	 		$this->success('修改流程步骤成功','__URL__/flow_process/type/'.$_POST['type_id']);
	 	}else{
	 		$this->error('修改流程步骤失败');
	 	}
	 }
	 /*****经办部门、经办角色 end*****/
	 
	 
	 public function flow_form(){
	 	$this->display();
	 }
	 
	 public function flow_form_list(){
	 	$workflow_form = M("workflow_form");
	 	$workflow_sort = M("workflow_sort");
	 	$data_sort = $workflow_sort->order('sort_no')->select();
	 	$data = array();
	 	$i = 0;
	 	foreach ($data_sort as $key=>$value){
	 		$sort_id = $value['sort_id'];
	 		$data_type = $workflow_form->where('sort_id='.$sort_id)->select();
	 		foreach ($data_type as $key=>$value2){
	 			$value['one'][$key] = $value2;
	 		}
	 		$data[] = $value;
	 		$i++;
	 	}
	 	$data_form0 = $workflow_form->where('sort_id=0')->select();
	 	if(count($data_form0)>0){//当存在未分组的流程
	 		$data[$i] = array(
	 			'sort_id'=>0,
	 			'sort_name'=>'未分类表单',
	 			'one'=>$data_form0
	 		);
	 	}
	 	
	 	$this->assign('data',$data);
	 	$this->display();
	 }
	 
	 public function flow_form_add(){
	 	$workflow_sort = M("workflow_sort");
	 	$data_sort = $workflow_sort->order('sort_no')->select();
	 	$this->assign('data_sort',$data_sort);
	 	$this->display();
	 }
	 
	 public function flow_form_insert(){
	 	if(!IS_POST){ $this->error("非法表单！"); }
	 	$sort_id = I('post.sort_id');
	 	if(!is_id($sort_id)){ $this->error("非法参数！"); }
	 	$data = array(
	 		'form_name'=>I('post.form_name'),
	 		'sort_id'=>$sort_id,
	 		'form_no'=>I('post.form_no'),
	 	);
	 	$workflow_form = M("workflow_form");
	 	$success = $workflow_form->add($data);
	 	if($success){
	 		$this->systemlog('add', '工作流设置', '添加表单---'.I('post.form_name'));
	 		$this->success('添加表单成功','flow_form_add');
	 	}else{
	 		$this->error('添加表单失败');
	 	}
	 }
	 
	 public function flow_form_sort_list(){
	 	$sort_id = I('get.sort_id') ? I('get.sort_id') : 1;
	 	
	 	if(!is_id($sort_id)){ $this->error("非法参数！"); }
	 	
	 	$workflow_form = M("workflow_form");
	 	$workflow_sort = M("workflow_sort");
	 	$data_form = $workflow_form->where('sort_id=%d',$sort_id)->order('form_no')->select();
	 	if($sort_id == 0){
	 		$data_sort = array(
	 			'sort_id'=>0,
	 			'sort_name'=>'未分组表单'
	 		);
	 	}else{
		 	$data_sort = $workflow_sort->where('sort_id=%d',$sort_id)->find();
	 	}
	 	$this->assign('data_list',$data_form);
	 	$this->assign('data_sort',$data_sort);
	 	$this->assign('count',count($data_form));
	 	$this->display();
	 }
	 
	 public function flow_form_edit(){
	 	$form_id = I('get.form_id');
	 	if(!is_id($form_id)){ $this->error("非法参数！"); }
	 	
	 	$workflow_form = M("workflow_form");
	 	$workflow_sort = M("workflow_sort");
	 	$data_form = $workflow_form->where('form_id=%d',$form_id)->find();
	 	$data_sort = $workflow_sort->select();
	 	$this->assign('data_form',$data_form);
	 	$this->assign('data_sort',$data_sort);
	 	$this->assign('form_id',$form_id);
	 	$this->display();
	 }
	 
	 public function flow_form_save(){
	 	if(!IS_POST){ $this->error("非法表单！"); }
	 	$sort_id = I('post.sort_id');
	 	$form_id = I('post.form_id');
	 	if(!is_id($form_id) || !is_id($sort_id)){ $this->error("非法参数！"); }
	 	$data = array(
	 		'form_name'=>I('post.form_name'),
	 		'sort_id'=>$sort_id,
	 		'form_no'=>I('post.form_no'),
	 	);
	 	$workflow_form = M("workflow_form");
		$success = $workflow_form->where('form_id=%d',$form_id)->save($data);
	 	if($success){
	 		$this->systemlog('edit', '工作流设置', '修改表单---'.I('post.form_name'));
	 		$this->success('修改表单成功','flow_form_edit?form_id='.$form_id);
	 	}else{
	 		$this->error('修改表单失败');
	 	}
	 }
	 
	 public function flow_form_delete(){
	 	$sort_id = I('get.sort_id');
	 	$form_id = I('get.form_id');
	 	if(!is_id($form_id) || !is_id($sort_id)){ $this->error("非法参数！"); }
	 	
	 	$workflow_form = M("workflow_form");
	 	$form_name = $workflow_form->where('form_id=%d',$form_id)->getField('form_name');
		$success = $workflow_form->where('form_id=%d',$form_id)->delete();
	 	if($success){
	 		$this->systemlog('delete', '工作流设置', '删除表单---'.$form_name);
	 	}
	 }
	 
	 public function flow_form_info(){
	 	$form_id = I("get.form_id");
	 	if(!is_id($form_id)){ $this->error("非法参数！"); }
	 	$workflow_form = M("workflow_form");
	 	$data = $workflow_form->where('form_id=%d',$form_id)->find();
	 	$this->assign('data',$data);
	 	$this->display();
	 }
	 
	 public function flow_form_model_save(){
	 	$form_id = I("get.form_id");
	 	if(!is_id($form_id)){ $this->error("非法参数！"); }
	 	
	 	$workflow_form = M("workflow_form");
	 	$data_form = $workflow_form->where('form_id=%d',$form_id)->find();
	 	$html = str_replace("\"","&",$data_form['form_model']);
		$html = str_replace("\\","",$html);
	 	$this->assign('form_id',$data_form['form_id']);
	 	$this->assign('form_model',$html);
	 	$this->display('WorkFlowSys/cool_form/update');
	 }
	 
	 public function flow_form_model_edit(){//保存表单
//	 	date_default_timezone_set('PRC');
//		$time = date('Y-m-d');
//		$name = session('name');
//		$user_name = session('user_name');
//		$user = M("Oa_sys_user");
//		$dept = M("Oa_sys_dept");
//		$priv = M("Oa_sys_priv");
//		
//		$data_user = $user->where("user_name = '$user_name'")->find();
//		$dept_name = $dept->where('id='.$data_user['dept_id'])->getField('dept_name');
		
		
	 	$form_id = $_POST['form_id'];
	 	$data['form_model'] = $_POST['CONTENT'];
	 	$workflow_form = M("Oa_workflow_form");
	 	$workflow_form->where('form_id='.$form_id)->save($data);
//	 	$form_model = html_element($data['form_model']);
//	 	foreach ($form_model as $value){
//	 		$title = explode(" ",strstr($value, 'title='));
//	 		$form_item = substr($title[0], '6');//表title的值
//	 		if($form_item == '填单日期' || $form_item == '填表日期'){
//	 			$data_value['填表日期'] = array(
//	 				'title' => 1,//存在占位符
//	 				'type' => 'time',//占位符类型
//	 				'value' => $time
//	 			);
//	 		}elseif($form_item == '姓名'){
//	 			$data_value['姓名'] = array(
//	 				'title' => 1,//存在占位符
//	 				'type' => 'name',//占位符类型
//	 				'value' => session('name')
//	 			);
//	 		}elseif($form_item == '部门'){
//	 			$data_value['部门'] = array(
//	 				'title' => 1,//存在占位符
//	 				'type' => 'dept',//占位符类型
//	 				'value' => $dept_name
//	 			);
//	 		}elseif($form_item == '职务' || $form_item == '职位'){
//	 			$data_value[] = "{{职位}}";
//	 		}else{
//	 			$data_value[] = "";
//	 		}
//	 	}
//	 	$data_value = implode('***', $data_value);
//	 	echo $data_value;
//	 	dump($form_model);
//	 	exit;
	 	
	 	$this->success('修改表单成功','flow_form_model_save?form_id='.$form_id);
	 }
	 
	public function replace(){
		$html=$_POST['html'];
		$title_array=array();
		$index=0;
		$replace_array=array();
		$pattern_array=array();
		$pattern="/(<{1})(INPUT|TEXTAREA|IMG|SELECT).*? title=.*?(>{1})/";
		$j=preg_match_all($pattern,$html,$m);
		$m_clone=$m[0];
		//print_r($m[0]);
		//循环数组然后再判断里面的title
		for($i=0;$i<$j;$i++){
			preg_match("/title=.*? /",$m_clone[$i],$ms);
			$title=$ms[0];
			$title_array[$title][]=$m_clone[$i];
		}
		
		//循环数组对各个相同title数组中data_替换为data_加当前循环的索引
	
		foreach($title_array as $key=>$value){
			$index++;
			for($s=0;$s<count($title_array[$key]);$s++){
				$title_array[$key][$s]=preg_replace("/data_\d*/","data_".$index,$title_array[$key][$s]);
				$replace_array[]=$title_array[$key][$s];
			}
		}
		
		for($n=0;$n<count($m_clone);$n++){
			$m_clone[$n]="/".$m_clone[$n]."/";
		}
	
		$new_html=preg_replace($m_clone,$replace_array,$html);
		echo $new_html;
	}
	 
	 public function textfield(){
	 	$this->display('WorkFlowSys/cool_form/textfield');
	 }
	 
	 public function auto(){
	 	$this->display('WorkFlowSys/cool_form/auto');
	 }
	 
	 public function calc(){
	 	$this->display('WorkFlowSys/cool_form/calc');
	 }
	 
	 public function calendar(){
	 	$this->display('WorkFlowSys/cool_form/calendar');
	 }
	 
	 public function checkbox(){
	 	$this->display('WorkFlowSys/cool_form/checkbox');
	 }
	 
	 public function listmenu(){
	 	$this->display('WorkFlowSys/cool_form/listmenu');
	 }
	 
	 public function listview(){
	 	$this->display('WorkFlowSys/cool_form/listview');
	 }
	 
	 public function textarea(){
	 	$this->display('WorkFlowSys/cool_form/textarea');
	 }
	 
	 public function hyperlink(){
	 	$this->display('WorkFlowSys/cool_form/hyperlink');
	 }
	 
	 public function sign(){
	 	$this->display('WorkFlowSys/cool_form/sign');
	 }
    
    public function _empty($name){
    	$this->emptyaction($name);
    }
	
}
?>